package com.mysiteforme.admin.controller.user;

import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricityUserDefend;
import com.mysiteforme.admin.service.ElectricityUserDefendService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息维护表  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-09-26
 */
@Controller
@RequestMapping("/admin/electricityUserDefend")
public class ElectricityUserDefendController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricityUserDefendController.class);

    @Autowired
    private ElectricityUserDefendService electricityUserDefendService;

    @GetMapping("list")
    @SysLog("跳转用户信息维护表列表")
    public String list(){
        return "/admin/electricityUserDefend/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求用户信息维护表列表数据")
    public LayerData<ElectricityUserDefend> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricityUserDefend> layerData = new LayerData<>();
        EntityWrapper<ElectricityUserDefend> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        wrapper.orderBy("create_date",false);
        if(!map.isEmpty()){
            String creditCode = (String) map.get("creditCode");
            if(StringUtils.isNotBlank(creditCode)) {
                wrapper.like("credit_code",creditCode);
            }else{
                map.remove("creditCode");
            }

            String conmanyName = (String) map.get("conmanyName");
            if(StringUtils.isNotBlank(conmanyName)) {
                wrapper.like("conmany_name",conmanyName);
            }else{
                map.remove("conmanyName");
            }

        }
        Page<ElectricityUserDefend> pageData = electricityUserDefendService.selectPage(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增用户信息维护表页面")
    public String add(){
        return "/admin/electricityUserDefend/add";
    }

    @PostMapping("add")
    @SysLog("保存新增用户信息维护表数据")
    @ResponseBody
    public RestResponse add(ElectricityUserDefend electricityUserDefend){

        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("credit_code", electricityUserDefend.getCreditCode());
        entityWrapper.eq("del_flag", "0");
        List<ElectricityUserDefend> list = electricityUserDefendService.selectList(entityWrapper);
        if (list != null && list.size()>0 ){
            return RestResponse.failure("该社会统一信用代码已存在");
        }
        boolean  bool = electricityUserDefendService.insert(electricityUserDefend);
        if(bool){
            return RestResponse.success();
        }
        return RestResponse.failure("新增失败");
    }

    @GetMapping("edit")
    @SysLog("跳转编辑用户信息维护表页面")
    public String edit(Long id,Model model){
        ElectricityUserDefend electricityUserDefend = electricityUserDefendService.selectById(id);
        model.addAttribute("electricityUserDefend",electricityUserDefend);
        return "/admin/electricityUserDefend/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑用户信息维护表数据")
    public RestResponse edit(ElectricityUserDefend electricityUserDefend){
        if(null == electricityUserDefend.getId() || 0 == electricityUserDefend.getId()){
            return RestResponse.failure("ID不能为空");
        }
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("credit_code", electricityUserDefend.getCreditCode());
        entityWrapper.eq("del_flag", "0");
        ElectricityUserDefend  userDefend = electricityUserDefendService.selectOne(entityWrapper);

        if(userDefend != null && !userDefend.getId().equals(electricityUserDefend.getId())){
            return RestResponse.failure("该社会统一信用代码已存在");
        }else {
            electricityUserDefendService.updateById(electricityUserDefend);
            return RestResponse.success();
        }
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除用户信息维护表数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricityUserDefend electricityUserDefend = electricityUserDefendService.selectById(id);
        electricityUserDefend.setDelFlag(true);
        electricityUserDefendService.updateById(electricityUserDefend);
        return RestResponse.success();
    }

}
